package vip.zhy.faststart.framework.domain;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.http.HttpStatus;
import vip.zhy.faststart.framework.json.JSON;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class Response<T> {

    private String msg;
    private Integer code;
    private T data;

    public static <T> Response<T> ok() {
        return base(HttpStatus.OK, null);
    }

    public static <T> Response<T> ok(String msg) {
        return base(msg, HttpStatus.OK.value(), null);
    }

    public static <T> Response<T> ok(T data) {
        return base(HttpStatus.OK, data);
    }

    public static <T> Response<T> ok(String msg, T data) {
        return base(msg, HttpStatus.OK.value(), data);
    }

    public static <T> Response<T> error() {
        return base(HttpStatus.BAD_REQUEST, null);
    }

    public static <T> Response<T> error(T data) {
        return base(HttpStatus.BAD_REQUEST, data);
    }

    public static <T> Response<T> error(String msg) {
        return base(msg, HttpStatus.BAD_REQUEST.value(), null);
    }

    public static <T> Response<T> error(String msg, T data) {
        return base(msg, HttpStatus.BAD_REQUEST.value(), data);
    }

    public static <T> Response<T> unauthorized() {
        return base(HttpStatus.UNAUTHORIZED, null);
    }

    public static <T> Response<T> unauthorized(T data) {
        return base(HttpStatus.UNAUTHORIZED, data);
    }

    public static <T> Response<T> base(HttpStatus hs, T data) {
        return base(hs.getReasonPhrase(), hs.value(), data);
    }

    public static <T> Response<T> base(String msg, Integer code, T data) {
        return new Response<>(msg, code, data);
    }

    @Override
    public String toString() {
        return JSON.toJSON(this);
    }

}
